home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 129_01 / 210log.c < prev    next >
Text File  |  1985-03-09  |  18KB  |  615 lines

  1. /************************************************************************/
  2. /*                log.c                    */
  3. /*                                    */
  4. /*       userlog code for  Citadel bulletin board system        */
  5. /************************************************************************/
  6.  
  7. /************************************************************************/
  8. /*                history                 */
  9. /*                                    */
  10. /* 84Dec28 HAW    findPerson() moved from MSG.C to LOG.C.         */
  11. /* 84Dec12 HAW    Fixed kill acct. bug so killed acct doesn't become login*/
  12. /* 84Jun23 HAW&JLS  Eliminating unused local variables using CRF.    */
  13. /* 84Jun19 JLS    Fixed terminate so that Mail> doesn't screw up SYSOP.    */
  14. /* 84Apr04 HAW    Started upgrade to BDS C 1.50a.             */
  15. /* 83Feb27 CrT    Fixed login-in-Mail> bug.                */
  16. /* 83Feb26 CrT    Limited # new messages for new users.            */
  17. /* 83Feb18 CrT    Null pw problem fixed.                    */
  18. /* 82Dec06 CrT    2.00 release.                        */
  19. /* 82Nov03 CrT    Began local history file & general V1.2 cleanup     */
  20. /************************************************************************/
  21.  
  22. #include <210ctdl.h>
  23.  
  24. /************************************************************************/
  25. /*                contents                */
  26. /*                                    */
  27. /*    crypte()        encrypts/decrypts data blocks        */
  28. /*    findPerson()        load log record for named person    */
  29. /*    getLog()        loads requested userlog record        */
  30. /*    hash()            hashes a string to an integer        */
  31. /*    login()         is menu-level routine to log caller in    */
  32. /*    logInit()        builds the RAM index to userlog.buf    */
  33. /*    newPW()         is menu-level routine to change a PW    */
  34. /*    newUser()        menu-level routine to log a new caller    */
  35. /*    noteLog()        enters a userlog record into RAM index    */
  36. /*    putLog()        stores a logBuffer into citadel.log    */
  37. /*    PWSlot()        returns userlog.buf slot password is in */
  38. /*    slideLTab()        support routine for sorting logTab    */
  39. /*    sortLog()        sort userlog by time since last call    */
  40. /*    storeLog()        store data in log            */
  41. /*    strCmpU()        strcmp(), but ignoring case distinctions*/
  42. /*    terminate()        menu-level routine to exit system    */
  43. /*    zapLogFile()        erases & re-initializes userlog.buf    */
  44. /************************************************************************/
  45.  
  46. /************************************************************************/
  47. /*    crypte() encrypts/decrypts data blocks                */
  48. /*                                    */
  49. /*  This was at first using a full multiply/add pseudo-random sequence    */
  50. /*  generator, but 8080s don't like to multiply.  Slowed down I/O    */
  51. /*  noticably.    Rewrote for speed.                    */
  52. /************************************************************************/
  53. #define b    fpc1
  54. #define c    fi1
  55. #define s    fi2
  56. crypte(buf, len, seed)
  57. char      *buf;
  58. unsigned  len, seed;
  59. {
  60.     seed    = (seed + cryptSeed) & 0xFF;
  61.     b        = buf;
  62.     c        = len;
  63.     s        = seed;
  64.     for (;  c;    c--) {
  65.     *b++   ^= s;
  66.     s    = (s + CRYPTADD)  &  0xFF;
  67.     }
  68. }
  69.  
  70. /************************************************************************/
  71. /*    findPerson() loads log record for named person.         */
  72. /*    RETURNS: ERROR if not found, else log record #            */
  73. /************************************************************************/
  74. int findPerson(name, lBuf)
  75. char            *name;
  76. struct logBuffer    *lBuf;
  77. {
  78.     int  h, i, foundIt, logNo;
  79.  
  80.     h    = hash(name);
  81.     for (foundIt=i=0;  i<MAXLOGTAB && !foundIt;  i++) {
  82.     if (logTab[i].ltnmhash == h) {
  83.         getLog(lBuf, logNo = logTab[i].ltlogSlot);
  84.         if (strCmpU(name, lBuf->lbname) == SAMESTRING) {
  85.         foundIt = TRUE;
  86.         }
  87.     }
  88.     }
  89.     if (!foundIt)    return ERROR;
  90.     else         return logNo;
  91. }
  92.  
  93. /************************************************************************/
  94. /*    getLog() loads requested log record into RAM buffer        */
  95. /************************************************************************/
  96. getLog(lBuf, n)
  97. struct logBuffer    *lBuf;
  98. int            n;
  99. {
  100.     if (lBuf == &logBuf)   thisLog    = n;
  101.  
  102.     n    *= SECSPERLOG;
  103.  
  104.     seek(logfl, n, 0);
  105.     if (read(logfl, lBuf, SECSPERLOG) == -1) {
  106.     printf("?getLog-read fail");
  107.     }
  108.  
  109.     crypte(lBuf, (SECSPERLOG*SECTSIZE), n);    /* decode buffer    */
  110. }
  111.  
  112. /************************************************************************/
  113. /*    hash() hashes a string to an integer                */
  114. /************************************************************************/
  115. int hash(str)
  116. char *str;
  117. {
  118.     char toUpper();
  119.     int  h, i, shift;
  120.  
  121.     for (h=shift=0;  *str;  shift=(shift+1)&7, str++) {
  122.     h ^= (i=toUpper(*str)) << shift;
  123.     }
  124.     return h;
  125. }
  126.  
  127. /************************************************************************/
  128. /*    login() is the menu-level routine to log someone in        */
  129. /************************************************************************/
  130. login(password)
  131. char *password;    /* TRUE if parameters follow    */
  132. {
  133.     char getYesNo();
  134.     int  foundIt, ltentry;
  135.  
  136.     foundIt =     ((ltentry = PWSlot(password, /* load == */ TRUE)) != ERROR);
  137.  
  138.     if (foundIt && *password) {
  139.  
  140.     /* recite caller's name, etc:     */
  141.     mPrintf(" %s\n", logBuf.lbname);
  142.  
  143.     /* update userlog entries: */
  144.  
  145.     loggedIn     = TRUE;
  146.     setUp(TRUE);
  147.  
  148.     showMessages(NEWoNLY, FALSE);
  149.  
  150.     listRooms(/* doDull== */ !expert);
  151.  
  152.     outFlag = OUTOK;
  153.     if (
  154.         (
  155.         logBuf.lbId[MAILSLOTS-1]
  156.         -
  157.         (logBuf.lbvisit[   logBuf.lbgen[MAILROOM] & CALLMASK   ]+1)
  158.         < 0x8000
  159.         )
  160.         &&
  161.         logBuf.lbId[MAILSLOTS-1] - oldestLo       < 0x8000
  162.         &&
  163.         thisRoom != MAILROOM
  164.     )   {
  165.         mPrintf("\n  * You have private mail in Mail> *\n ");
  166.     }
  167.  
  168.     } else {
  169.     /* discourage password-guessing: */
  170.     if (strLen(password) > 1 && whichIO == MODEM)    pause(2000);
  171.     if (!unlogLoginOk  &&  whichIO == MODEM)  {
  172.         mPrintf(" No record -- leave message to 'sysop' in Mail>\n ");
  173.     } else if (getYesNo(" No record: Enter as new user"))    newUser();
  174.     }
  175. }
  176.  
  177. /************************************************************************/
  178. /*    logInit() indexes userlog.buf                    */
  179. /************************************************************************/
  180. logInit()
  181. {
  182.     int i;
  183.     int count;
  184.  
  185.     count = 0;
  186.  
  187.     /* clear logTab */
  188.     for (i=0;  i<MAXLOGTAB;  i++) logTab[i].ltnewest    = ERROR;
  189.  
  190.     /* load logTab: */
  191.     for (thisLog=0;  thisLog<MAXLOGTAB;  thisLog++) {
  192.     printf("log#%d", thisLog);
  193.     getLog(&logBuf, thisLog);
  194.  
  195.     /* count valid entries:         */
  196.     if (logBuf.lbvisit[0] != ERROR) {
  197.         count++;
  198.         printf("  %s", logBuf.lbname);
  199.     }
  200.     putChar('\n');
  201.  
  202.     /* copy relevant info into index:   */
  203.     logTab[thisLog].ltnewest = logBuf.lbvisit[0];
  204.     logTab[thisLog].ltlogSlot= thisLog;
  205.     logTab[thisLog].ltnmhash = hash(logBuf.lbname);
  206.     logTab[thisLog].ltpwhash = hash(logBuf.lbpw  );
  207.     }
  208.     printf(" logInit--%d valid log entries\n", count);
  209.     sortLog();
  210. }
  211.  
  212. /************************************************************************/
  213. /*    newPW() is menu-level routine to change one's password        */
  214. /*    since some Citadel nodes run in public locations, we avoid    */
  215. /*    displaying passwords on the console.                */
  216. /************************************************************************/
  217. newPW()
  218. {
  219.     char oldPw[NAMESIZE];
  220.     char pw[NAMESIZE];
  221.     char *s;
  222.     int  goodPW;
  223.  
  224.     /* save password so we can find current user again: */
  225.     if (!loggedIn) {
  226.     mPrintf("?Can't change pwd when not logged in!\n ");
  227.     return ;
  228.     }
  229.     strcpy(oldPw, logBuf.lbpw);
  230.     storeLog();
  231.     do {
  232.     echo    = CALLER;
  233.     getNormStr(" new password", pw, NAMESIZE);
  234.     echo    = BOTH;
  235.  
  236.     /* check that PW isn't already claimed: */
  237.     goodPW = (PWSlot(pw, /* load == */ TRUE) == ERROR  &&  strlen(pw) >= 2);
  238.  
  239.     if (!goodPW) mPrintf("\n Poor password\n ");
  240.  
  241.     } while (!goodPW && (haveCarrier || whichIO==CONSOLE));
  242.  
  243.     doCR();
  244.     PWSlot(oldPw, /* load == */TRUE);    /* reload old log entry         */
  245.     pw[NAMESIZE-1] = 0x00;        /* insure against loss of carrier:  */
  246.  
  247.     if (goodPW    &&  strlen(pw) > 1) {    /* accept new PW:            */
  248.     strcpy(logBuf.lbpw, pw);
  249.     logTab[0].ltpwhash    = hash(pw);
  250.     }
  251.  
  252.     mPrintf("\n %s\n pw: ", logBuf.lbname);
  253.     echo = CALLER;
  254.     mPrintf("%s\n ", logBuf.lbpw);
  255.     echo = BOTH;
  256. }
  257.  
  258. /************************************************************************/
  259. /*    newUser() prompts for name and password             */
  260. /***********************